<template>
	<view class="more">
		<view class="search-view">
			<u-search placeholder="搜索" :disabled="true" :show-action="false" @click="clickSearch" height="37">
			</u-search>
		</view>
		<view class="swiper-view">
			<u-swiper :list="swiperImgArray" :autoplay="true" radius="0" height="232rpx">
				<view slot="indicator" class="indicator">
					<view class="indicator__dot" v-for="(item, index) in swiperImgArray" :key="index"
						:class="[index === current && 'indicator__dot--active']">
					</view>
				</view>
			</u-swiper>
		</view>
		<view class="screen">
			<!-- <view class="screen-bar">
				<view class="sreen-left">
					<view class="comp" @tap="screenCurrent=0" :class="{active: screenCurrent === 0}">
						综合
					</view>
					
					<view class="sales" @tap="screenCurrent=1" :class="{active: screenCurrent === 1}">
						销量
					</view>
				</view>
				
				<view class="filter">
					筛选
				</view>
			</view> -->

			<!-- <view class="commodity-list" v-if="goodsList">
				<more-item :item="item" v-for="(item, index) in goodsList" :key="item.id" />
			</view> -->

			<view class="footer-goods">
				<view class="screen-bar">
					<view class="sreen-left">
						<view class="all" @tap="all" :class="{active: screenCurrent === 0}">
							{{$t('home.all')}}
						</view>

						<view class="sold" @tap="sold" :class="{active: screenCurrent === 1}">
							{{$t('home.count')}}
						</view>

						<view class="on-sale" @tap="onSale" :class="{active: screenCurrent === 2}">
							{{$t('home.onSold')}}
						</view>
					</view>

					<view class="filter" @tap="showFilter">
						<view class="filter-desc">
							{{$t('home.filter')}}
						</view>
						<image class="screen-img" src="../../static/icon/image_88.png" mode=""></image>
					</view>
				</view>
				
				<view class="goods-list" v-if="changeGoodsList.length > 0">
					<waterfallsFlow imageSrcKey="seriesOne" :list="changeGoodsList">
						<template v-slot:default="item">
							<GoodsItem :isNoImg="true" :isClick="true" :goodsItem="item">
							</GoodsItem>
						</template>
					 </waterfallsFlow >
				</view>
				
				<u-loading-icon v-else></u-loading-icon>
			</view>

			<u-popup :show="rightShow" mode="right" @close="rightShow=false" closeOnClickOverlay>
				<view class="side" :style="{height: phoneHeight}">
					<view class="filter-item" v-for="item in filterList" :key="item.id">
						<view class="title-item">
							{{$t('language')=="zh"?item.brandName:item.brandEngName}}
						</view>

						<view class="select-list">
							<view class="select-item" :class="{'select-active': selected.indexOf(subItem.id)!==-1}"
								@tap="selectActive(subItem.id)" v-for="subItem in item.children" :key="subItem.id">
								{{$t('language')=="zh"?subItem.brandName:subItem.brandEngName}}
							</view>
						</view>
					</view>

					<view class="filter-pla">

					</view>

					<view class="side-footer">
						<view class="reset-btn" @tap="selected = []">
							{{$t('home.reset')}}
						</view>

						<view class="confirm-btn" @tap="filterConfirm">
							{{$t('home.confirm')}}
						</view>
					</view>
				</view>
			</u-popup>
		</view>

	</view>
</template>

<script>
	import search from '@/components/search.vue'
	import moreItem from '@/components/moreItem.vue'
	import {
		getSwiper,
		getCategoryGoods,
		getCategory,
		getAllGoodsList
	} from "@/common/api.js"
	import waterfallsFlow from "@/components/maramlee-waterfalls-flow/maramlee-waterfalls-flow.vue";

	export default {
		components: {
			search,
			moreItem,
			waterfallsFlow
		},
		data() {
			return {
				swiperImgArray: [],
				current: 0,
				screenCurrent: 0,
				item: null,
				goodsList: null,
				rightShow: false,
				phoneHeight: null,
				changeGoodsList: [],
				filterList: [],
				selected: [],
				pageSize: 10,
				pageNum: 1,
				status: null
			}
		},
		methods: {
			selectActive(item) {
				if (this.selected.indexOf(item) !== -1) {
					this.selected.splice(this.selected.indexOf(item), 1); //取消
				} else {
					this.selected.push(item); //选中添加到数组里
				}
				console.log(this.selected, item, this.selected.indexOf(item));
			},
			initChangeGoodsList() {
				this.changeGoodsList = [...this.changeGoodsList, ...this.goodsList];
			},
			async filterConfirm() {
				const res = await getAllGoodsList({
					pageSize: 1000,
					classIds: this.selected.join(","),
					classificationId: this.item.id + ","
				});
				
				this.goodsList = res.rows;
				this.initChangeGoodsList();
				this.rightShow = false;
			},
			onReachBottom() {
				this.initData(); 
			},
			clickSearch() {
				uni.navigateTo({
					url: "/pages/search/search"
				})
			},
			//轮播图获取
			async getSwiperImg() {
				const res = await getSwiper({
					parentId: 1
				});
				if (res.code == 200) {
					res.rows.forEach(e => {
						this.swiperImgArray.push(e.url)
					})
				}
			},
			async initFilter() {
				const res = await getCategory();
				console.log(res);
				const tempBrand = res.splice(0, 1);
				// console.log(tempBrand);
				res.forEach((item) => {
					if (Array.isArray(item.children)) {
						this.filterList = [...this.filterList, ...item.children]
					}
				});
				console.log(this.filterList);
			},
			async initData() {
				const res = await getCategoryGoods({
					pageSize: this.pageSize,
					pageNum: this.pageNum,
					status: this.status,
					classificationId: this.item.id + ","
				})
				console.log(res);
				if(res.code == 200) {
					this.pageNum++;
					this.goodsList = res.rows;
					this.initChangeGoodsList();
				}
				// console.log(this.goodsList);
			},
			all() {
				this.screenCurrent = 0;
				this.status = null;
				this.pageNum = 1;
				this.changeGoodsList = [];
				this.initData();
			},
			sold() {
				this.screenCurrent = 1;
				this.status = 3;
				this.pageNum = 1;
				this.changeGoodsList = [];
				this.initData();
			},
			onSale() {
				this.screenCurrent = 2;
				this.status = 1;
				this.pageNum = 1;
				this.changeGoodsList = [];
				this.initData();
			},
			showFilter() {
				// console.log(123123123);
				this.rightShow = true;
			},
		},
		onReady() {
			uni.setNavigationBarTitle({
				title: this.$t("categoryShow.title")
			});
		},
		onLoad(option) {
			this.getSwiperImg();
			this.item = JSON.parse(decodeURIComponent(option.item));
			console.log(this.item);
			this.initData();
			this.initFilter();
			console.log(this.item);
			let self = this;
			uni.getSystemInfo({
				success(res) {
					self.phoneHeight = (res.screenHeight * (750 / res.windowWidth)) + 100 + "rpx";
				}
			});
		}
	}
</script>

<style scoped lang="scss">
	.swiper-view {
		margin: 14rpx 0px;
		margin-top: 102rpx;
	}

	.item {
		display: flex;
	}

	.search-view {
		padding: 14rpx 22rpx;
		position: fixed;
		/* #ifdef APP */
		top: 0;
		/* #endif */
		
		/* #ifdef H5 */
		top: 86rpx;
		/* #endif */
		left: 0;
		right: 0;
		z-index: 1;
		background-color: #fff;
	}

	.screen-bar {
		width: 100%;
		border-radius: 20rpx 20rpx 0rpx 0rpx;
		background: #FFFFFF;
		box-shadow: 0rpx -2rpx 16rpx 0rpx #0000000C;
		height: 88rpx;
		display: flex;
		align-items: center;
		color: #717184;
		font-family: PingFang SC;
		font-weight: bold;
		font-size: 26rpx;

		.sreen-left {
			width: 80%;
			display: flex;
			align-items: center;
			justify-content: center;
			border-right: 5rpx solid #858585;

			.comp {
				margin-right: 155rpx;
			}

			.sales {}
		}

		.filter {
			width: 20%;
			margin-left: 30rpx;
		}
	}

	.active {
		color: #FAAD67;
		font-family: PingFang SC;
		font-weight: bold;
		font-size: 26rpx;
	}

	.screen {
		width: 100%;
		background: #FBFBFB;
		box-shadow: 0px -1px 8px rgba(0, 0, 0, 0.1);
		border-radius: 10px;
	}

	.more {
		padding: 0rpx 24rpx;
		width: 100%;
		box-sizing: border-box;
	}

	.indicator {
		@include flex(row);
		justify-content: center;

		&__dot {
			height: 6px;
			width: 6px;
			border-radius: 100px;
			background-color: rgba(255, 255, 255, 0.35);
			margin: 0 5px;
			transition: background-color 0.3s;

			&--active {
				background-color: #ffffff;
			}
		}
	}

	.commodity-list {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding: 0 30rpx;
	}

	.goods-list {
		display: flex;
		align-items: center;
		justify-content: space-between;
		flex-wrap: wrap;
		padding: 0rpx 20rpx;
		padding-bottom: 20rpx;
		box-sizing: border-box;
	}

	.footer-goods {
		border-radius: 20rpx;
		background: #FBFBFB;
		box-shadow: 0rpx 0rpx 16rpx 0rpx #00000019;
	}
	
	.u-loading-icon {
		padding: 30rpx 0;
	}

	.side {
		width: 600rpx;
		// height: 100%;
		overflow-y: scroll;
		padding-bottom: 100rpx;

		.filter-pla {
			height: 200rpx;
			width: 0;
		}

		.side-footer {
			position: fixed;
			bottom: 0;
			width: 600rpx;
			height: 88rpx;
			display: flex;
			align-items: center;

			.reset-btn {
				width: 50%;
				height: 100%;
				background: #FFFFFF;
				box-shadow: 0px 1px 4px rgba(0, 0, 0, 0.25);
				font-family: 'PingFang SC';
				font-style: normal;
				font-weight: 700;
				font-size: 30rpx;
				color: #858585;
				text-align: center;
				line-height: 88rpx;
			}

			.confirm-btn {
				width: 50%;
				height: 100%;
				font-family: 'PingFang SC';
				font-style: normal;
				font-weight: 700;
				font-size: 30rpx;
				color: #fff;
				text-align: center;
				line-height: 88rpx;
				background-color: #000000;
			}
		}

		.filter-item {
			padding-left: 40rpx;
			margin-top: 40rpx;

			.title-item {
				font-family: 'PingFang SC';
				font-style: normal;
				font-weight: 700;
				font-size: 30rpx;
			}

			.select-list {
				display: flex;
				align-items: center;
				flex-wrap: wrap;

				.select-item {
					margin: 0 15rpx;
					margin-top: 20rpx;
					// width: 150rpx;
					padding: 0 40rpx;
					height: 60rpx;
					font-family: 'PingFang SC';
					font-style: normal;
					font-weight: 700;
					font-size: 20rpx;
					color: #858585;
					text-align: center;
					line-height: 60rpx;
					background: #F6F6F6;
					border-radius: 40rpx;
				}
			}
		}
	}

	.select-active {
		background: #FAAD67 !important;
		color: #fff !important;
	}

	.screen-bar {
		width: 100%;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 0 0;
		height: 88rpx;
		display: flex;
		align-items: center;
		color: #717184;
		font-family: PingFang SC;
		font-weight: bold;
		font-size: 26rpx;

		.sreen-left {
			width: 82%;
			display: flex;
			align-items: center;
			justify-content: center;
			border-right: 5rpx solid #858585;

			.all {
				margin-right: 88rpx;
			}

			.sold {
				margin-right: 88rpx;
			}
		}

		.filter {
			width: 18%;
			margin-left: 30rpx;
			display: flex;
			align-items: center;

			.screen-img {
				width: 30rpx;
				height: 30rpx;
			}
		}
	}

	.active {
		color: #FAAD67;
		font-family: PingFang SC;
		font-weight: bold;
		font-size: 26rpx;
	}
</style>
